package com.searademo.products.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.searademo.products.entity.StorageTbl;
import com.searademo.products.mapper.StorageTblMapper;
import com.searademo.products.service.IStorageTblService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuguohua
 * @since 2021-09-01
 */
@Service
public class StorageTblServiceImpl extends ServiceImpl<StorageTblMapper, StorageTbl> implements IStorageTblService {

    /**
     * 减库存
     *
     * @param commodityCode
     * @param count
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deduct(String commodityCode, Integer count) {
        if (commodityCode.equals("product-2")) {
            throw new RuntimeException("异常:模拟业务异常:Storage branch exception");
        }

        QueryWrapper<StorageTbl> wrapper = new QueryWrapper<>();
        wrapper.setEntity(new StorageTbl().setCommodityCode(commodityCode));
        StorageTbl storageTbl = baseMapper.selectOne(wrapper);
        storageTbl.setCount(storageTbl.getCount() - count);

        baseMapper.updateById(storageTbl);
    }
}
